package com.miguo.utils;

import android.content.Context;

import com.miguo.app.AppContext;
import com.miguo.bean.CategoryBean;
import com.miguo.bean.CategoryBrandBean;
import com.miguo.bean.CategoryThemeBean;
import com.miguo.bean.CommentBean;
import com.miguo.db.CategoryBrandDB;
import com.miguo.db.CategoryDB;
import com.miguo.db.CategoryThemeDB;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.request.RequestJsonHandler;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;

import java.util.List;

public class Category {

    private ApiPrefs prefs;

    private Context mContext;
    private boolean isVersionMark = false;
    private static Category instance;

    private List<CategoryBean> CategoryList;
    private List<CategoryThemeBean> CategoryThemeList;
    private List<CategoryBrandBean> CategoryBrandList;

    private Long getTime;
    private Long CateTime;

    private Category(Context context, boolean isVersionMark) {
        this.mContext = context;
        this.isVersionMark = isVersionMark;
    }

    public static Category getInstance(Context context, boolean isVersionMark) {
        if (instance == null) {
            instance = new Category(context, isVersionMark);
        }
        return instance;
    }

    public void init() {
        prefs = AppContext.Prefs();
        getTime = DateUtils.getCurrentDateLong("yyyyMMdd");
        CateTime = prefs.getCategoryTime();
        if (getTime > CateTime || isVersionMark) {
            getList();
        }
    }

    private void getList() {
        RequestJsonHandler handler = new RequestJsonHandler() {
            @Override
            public void onRequestFinish(JsonData json) {
                JsonData data = json.optJson("data");
                JsonData category = data.optJson("category");
                JsonData theme = data.optJson("theme");
                JsonData brand = data.optJson("brand");
                CategoryList = CommentBean.Category(category);
                if (CategoryList != null && CategoryList.size() > 0) {
                    CategoryDB.getInstance(mContext, false).deleteAll();
                    CategoryDB.getInstance(mContext, false).saveList(CategoryList);
                }

                CategoryThemeList = CommentBean.CategoryTheme(theme);
                if (CategoryThemeList != null && CategoryThemeList.size() > 0) {
                    CategoryThemeDB.getInstance(mContext, false).deleteAll();
                    CategoryThemeDB.getInstance(mContext, false).saveList(CategoryThemeList);
                }

                CategoryBrandList = CommentBean.CategoryBrand(brand);
                if (CategoryBrandList != null && CategoryBrandList.size() > 0) {
                    CategoryBrandDB.getInstance(mContext, false).deleteAll();
                    CategoryBrandDB.getInstance(mContext, false).saveList(CategoryBrandList);
                }
                //添加时间2016-04-27 10:43,通知首页更新数据
//                EventBus.getDefault().post(new CategoryLoadFinishedEvent(CategoryList));
                //------  END ----------

                prefs.setCategoryTime(getTime);

            }
        };
        HttpClient.post(Url.toRoute(Url.Category.List, 1), handler);
    }
}
